﻿/*
 * 留言Controller类
 * 创建时间：2012-1-11
 * 创建人：付昱
 * 版本：1.0
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HYBYSoft.Company.Models;

namespace HYBYSoft.Company.WebSite.Areas.Admin.Controllers
{
    public class QuestionMessagesController : Controller
    {
        /// <summary>
        /// 数据实体上下文对象
        /// </summary>
        CompanySiteEntities _dbContext = new CompanySiteEntities();

        /// <summary>
        /// 留言管理首页面
        /// </summary>
        /// <returns></returns>
        [Authentication]
        public ActionResult Index(string titles,DateTime? start,DateTime? end,int? isDelete)
        {
            //留言内容查询
            string content = titles??string.Empty;
            //留言发布的开始时间参数
            DateTime startTime = start.HasValue ? start.Value : DateTime.MinValue;
            //留言发布的结束时间参数
            DateTime endTime = end.HasValue ? end.Value : DateTime.MaxValue;
            //留言是否是删除状态
            int state = isDelete.HasValue ? isDelete.Value : 2;

            //根据条件进行查询
            var questions = from question in _dbContext.QuestionMessages
                            where (state == 2 ? true : question.State == state) && question.Titles.Contains(content) && (question.CreateTime >= startTime && question.CreateTime <= endTime) 
                            orderby question.CreateTime descending
                            select question;

            return View(questions);
        }

        /// <summary>
        /// 条件查询留言信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            return RedirectToAction("Index", new { titles = Request["Titles"], start = Request["StartTime"], end = Request["EndTime"], isDelete = Request["IsDelete"] });
        }

        /// <summary>
        /// 回复留言
        /// </summary>
        /// <param name="id">留言id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ReplyQuestion(int id)
        {
            //创建留言/答复实例
            ReplyMessage replyMessage = new ReplyMessage();
            replyMessage.QuestionID = id;
            replyMessage.Contents = Request["Contents"];
            replyMessage.CreateTime = DateTime.Now;
            replyMessage.IsAdmin = 1;

            //通过留言ID获取留言信息
            QuestionMessage message = _dbContext.QuestionMessages.FirstOrDefault(questionMesage => questionMesage.QuestionID == id);
            //更新问题回复状态和最后回复时间
            message.IsReply = 1;
            message.LastReplyTime = replyMessage.CreateTime.Value;

            //新增留言回复信息
            _dbContext.AddToReplyMessages(replyMessage);
            _dbContext.SaveChanges();

            return RedirectToAction("Index");
        }

        /// <summary>
        /// 显示留言相关信息
        /// </summary>
        /// <param name="id">留言id</param>
        /// <returns></returns>
        [Authentication]
        public ActionResult Details(int id)
        {
            //通过留言ID查询一个留言的详细信息以及该留言的回复信息
            var questionAndReply = (from question in _dbContext.QuestionMessages
                                    join replyMessage in _dbContext.ReplyMessages on question.QuestionID equals replyMessage.QuestionID into questionAndReplyMessage
                                    where question.QuestionID == id
                                    select new QuestionAndReplyMessages
                                    {
                                        QuestionID = question.QuestionID,
                                        Titles = question.Titles,
                                        CreateTime = question.CreateTime,
                                        LastReplyTime = question.LastReplyTime,
                                        State = question.State,
                                        IsReply = question.IsReply,
                                        ReplyMessages = questionAndReplyMessage
                                    }).First();

            return View(questionAndReply);
        }

        /// <summary>
        /// 回复留言
        /// </summary>
        /// <param name="id">留言id</param>
        /// <returns></returns>
        [Authentication]
        public ActionResult Reply(int id)
        {
            int questionID = (int)id;
            //通过ID查询该留言以及后续问题
            var questionAndReply = (from question in _dbContext.QuestionMessages
                                    join replyMessage in _dbContext.ReplyMessages on question.QuestionID equals replyMessage.QuestionID into questionAndReplyMessage
                                    where question.QuestionID == questionID
                                    select new QuestionAndReplyMessages
                                    {
                                        QuestionID = question.QuestionID,
                                        Titles = question.Titles,
                                        CreateTime = question.CreateTime,
                                        LastReplyTime = question.LastReplyTime,
                                        State = question.State,
                                        IsReply = question.IsReply,
                                        ReplyMessages = questionAndReplyMessage
                                    }).First();
            return View(questionAndReply);
        }

        /// <summary>
        /// 删除留言
        /// </summary>
        /// <param name="id">留言id</param>
        /// <returns></returns>
        [Authentication]
        public ActionResult Delete(int id)
        {
            //查询留言信息
            var questionMessage = (from question in _dbContext.QuestionMessages
                                   where question.QuestionID == id
                                   select question).First();
            //修改状态为删除
            questionMessage.State = 0;

            _dbContext.SaveChanges();
            return RedirectToAction("Index");
        }

    }
}
